#include <iostream>
#include <algorithm>
using namespace std;

#define LL long long

const LL mod = 1e9 + 7;

LL f(LL n)
{
    LL sum = 0;
    if (n == 1)
        return 1;
    else
    {
        for (LL i = 1; i <= n - 1; i++)
        {
            sum += (__gcd(i, n - i) == 1);
        }
    }
    return sum % mod;
}

LL g(LL n)
{
    LL sum = 0;
    for (LL i = 1; i <= n; i++)
    {
        if (n % i == 0)
        {
            sum += f(n / i);
        }
    }
    return sum % mod;
}

LL G(LL n, LL k)
{
    if (k == 1)
        return f(g(n));
    else if (k > 1 && k % 2 == 0)
    {
        return g(G(n, k - 1));
    }
    else if(k>  1 && k%2==1)
    {
        return f(G(n, k - 1));
    }
    else{
        return 0;
    }
}

int main()
{
    int T;
    cin>>T;
    LL n,k;
    for(int i=0;i<T;i++){
        cin>>n>>k;
        cout<<G(n,k)<<endl;
    }
    return 0;
}